﻿using UnityEngine;
using System.Collections;
using evt;
using UnityEngine.SceneManagement;
using tools;
using info;
public class CreateRoomView : MonoBehaviour {

	// Use this for initialization
	public int Score = 0;
	public int Number = 0;
	private int rate = 0;
	private Transform ScoreTran = null;
	private Transform NumTran = null;
	private Transform Rate = null;

	void Start () {
				
		ServerEventDispather.getInstance ().AddEventDispather (ServerEvent.SHOW_CREATEROOM_VIEW,OpenView);
		ServerEventDispather.getInstance ().AddEventDispather (ServerEvent.CREATE_ROOM_RESULT,CreateRoomResult);
		EventTriggerListener.Get (gameObject).onClick = HideView;
		EventTriggerListener.Get (transform.GetChild(0).gameObject).onClick = OpenView;
		EventTriggerListener.Get (transform.GetChild(1).gameObject).onClick = OpenView;
		EventTriggerListener.Get (transform.GetChild(5).gameObject).onClick = ToRecharge;
		EventTriggerListener.Get (gameObject).onClick = HideView;

		ScoreTran = transform.GetChild (4).GetChild(0);
		NumTran = transform.GetChild (4).GetChild(1);
		Rate =  transform.GetChild (4).GetChild(16);

		for( int i = 0 ; i < ScoreTran.childCount ; i++ ){
			EventTriggerListener.Get (ScoreTran.GetChild (i).gameObject).onClick = ChangState;
		}

		for( int i = 0 ; i < NumTran.childCount ; i++ ){
			EventTriggerListener.Get (NumTran.GetChild (i).gameObject).onClick = ChangeNum;
		}

		for( int i = 0 ; i < Rate.childCount ; i++ ){
			EventTriggerListener.Get (Rate.GetChild (i).gameObject).onClick = ChangeRate;
		}
	}

	private void ToRecharge(object data){
		int[] arr = new int[3];
		arr [0] = (Score+1) * 10;
		arr [1] = Number+2;
		arr [2] = rate;
		GSocket.Send(ByteFactory.makeByte(621105,arr));
	}	

	private void CreateRoomResult(object data){
		ServerEventDispather.getInstance ().removeAll ();
		SceneManager.LoadScene (3);
	}
	
	private void OpenView(object data){
		transform.localPosition = Vector3.zero;
	}

	private void HideView(object data){
		transform.localPosition = new Vector3(10000,0,0);
	}

	private void ChangState(GameObject go){
		SoundPlayer.play ("7571");
		go.GetComponent<Checkbox> ().onSelect = true;
		Score = go.GetComponent<Checkbox> ().type;
		for( int i = 0 ; i < ScoreTran.childCount ; i++ ){
			if( ScoreTran.GetChild(i).GetComponent<Checkbox> ().type != Score ){
					ScoreTran.GetChild (i).GetComponent<Checkbox> ().onSelect = false;
			}
		}
	}

	private void ChangeNum(GameObject go){
		SoundPlayer.play ("7571");
		go.GetComponent<Checkbox> ().onSelect = true;
		Number = go.GetComponent<Checkbox> ().type;
		for( int i = 0 ; i < NumTran.childCount ; i++ ){
			if( NumTran.GetChild(i).GetComponent<Checkbox> ().type != Number ){
					NumTran.GetChild (i).GetComponent<Checkbox> ().onSelect = false;
			}
		}
	}

	private void ChangeRate(GameObject go){
		SoundPlayer.play ("7571");
		go.GetComponent<Checkbox> ().onSelect = true;
		rate = go.GetComponent<Checkbox> ().type;
		for( int i = 0 ; i < Rate.childCount ; i++ ){
			if( Rate.GetChild(i).GetComponent<Checkbox> ().type != Number ){
				Rate.GetChild (i).GetComponent<Checkbox> ().onSelect = false;
			}
		}
	}

	// Update is called once per frame
	void Update () {

	}
}
